package edu.zqu.collegeField.project.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import edu.zqu.collegeField.framework.easyexcel.listener.ExcelListener;
import edu.zqu.collegeField.framework.easyexcel.model.FieldModel;
import edu.zqu.collegeField.framework.easyexcel.utils.ExcelWriteUtil;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.service.FieldService;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 * 场地 前端控制器
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@RestController
@RequestMapping("/api")
@Api(tags = "场地信息")
public class FieldController {
    @Autowired
    private FieldService fieldService;

    @ApiOperation(value = "获取场地信息列表")
    @GetMapping(value = "/fields")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "场地名称", paramType = "query", required = false),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = false),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = false),
            @ApiImplicitParam(name = "areaId", value = "功能区id", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId,
                         String name,
                         Integer fieldTypeId,
                         BigInteger schoolId,
                         BigInteger areaId
    ) {
        List<FieldVo> fieldVoList = fieldService.get(collegeId, StrUtil.trimToNull(name), fieldTypeId, schoolId, areaId);
        if (StringUtils.isNotEmpty(fieldVoList)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldVoList);
        } else return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }

    @ApiOperation(value = "获取场地信息", response = FieldVo.class)
    @GetMapping(value = "/field/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        FieldVo fieldVo = fieldService.getById(id);
        if (StringUtils.isNotNull(fieldVo)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldVo);
        }
        return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }


    @ApiOperation(value = "获取场地名称", response = FieldVo.class)
    @GetMapping(value = "/fieldName/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地id", paramType = "path", required = true)
    })
    public ZQUResult getNameById(@PathVariable BigInteger id) {
        String fieldName = fieldService.getNameById(id);
        if (StringUtils.isNotNull(fieldName)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldName);
        }
        return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/field")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Field", name = "Field", value = "场地对象", paramType = "query", required = true)
    })
    public ZQUResult add(@RequestBody Field field) {
        int rs = fieldService.add(field);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "添加成功", null);
        }
        return new ZQUResult(HttpStatus.ERROR, "添加失败", null);
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/field")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Field", name = "Field", value = "场地对象", paramType = "query", required = true)
    })
    public ZQUResult update(@RequestBody Field field) {
        int rs = fieldService.update(field);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "修改成功", null);
        }
        return new ZQUResult(HttpStatus.ERROR, "修改失败", null);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/field/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        int rs = fieldService.delete(id, byWho);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "删除成功", null);
        }
        return new ZQUResult(HttpStatus.ERROR, "删除失败", null);
    }

    @GetMapping("/field/download")
    @ApiOperation(value = "获取场地批量导入模板")
    public void downloadExcel(HttpServletResponse response) throws Exception {
        ExcelWriteUtil.simpleWrite(response, FieldModel.class, "场地批量导入模板", "模板", null);
    }

    @PostMapping("/field/batch")
    @ResponseBody
    @ApiOperation(value = "批量导入场地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "操作者", paramType = "query", required = true)
    })
    public ZQUResult insertBatch(MultipartFile file, BigInteger collegeId, BigInteger byWho) throws IOException {
        ExcelListener excelListener = new ExcelListener();
        EasyExcel.read(file.getInputStream(), FieldModel.class, excelListener)
                .sheet().headRowNumber(1).doRead();
        if (StringUtils.isNotEmpty(excelListener.getExcelData().getErrorRows())) {
            return new ZQUResult(HttpStatus.ERROR, "导入失败", excelListener.getExcelData().getErrorRows());
        }
        fieldService.insertBatch(excelListener.getExcelData().getRows(), collegeId, byWho);
        return new ZQUResult(HttpStatus.SUCCESS, "导入成功", null);
    }
}

